﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using Microsoft.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.AssginRule
{
    /// <summary>
    /// ClassTree 的摘要说明。
    /// </summary>
    [CLSCompliant(false)]
    public partial class ClassTreePage : Page
    {
        protected EntityType DisplayType
        {
            get;
            private set;
        }

        protected int SrcID
        {
            get;
            private set;
        }

        protected override void OnLoad(EventArgs e)
        {
            int value;
            if (!int.TryParse(Request.QueryString["SrcID"], out value))
            {
                value = 0;
            }
            SrcID = value;

            if (!int.TryParse(Request.QueryString["DisplayType"], out value))
            {
                value = (int)EntityType.Position;
            }
            DisplayType = (EntityType)value;

            if (!Page.IsPostBack)
            {
                var dt = InitRootNodeDataTable();
                InitTreeRootNode(classTreeView.Nodes, dt);
                classTreeView.ExpandLevel = 1;
            }
            base.OnLoad(e);
        }

        /// <summary>
        /// 初始化 RootNode DataTable
        /// </summary>
        private DataTable InitRootNodeDataTable()
        {
            var data = new Database();
            String username = SiteContext.Current.UserName;
            var prams = new[]
            {
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 20, username)
            };
            using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetShowClass_V2", prams))
            {
                var dt = Tools.ConvertDataReaderToDataTable(dataReader);
                dt.TableName = "TreeView";

                return dt;
            }
        }

        /// <summary>
        /// 初始化 ChildNode DataTable
        /// </summary>
        private DataTable InitChildNodeDataTable(int ClassParentID)
        {
            var data = new Database();
            var prams = new[]
            {
                Database.MakeInParam("@Class_id", SqlDbType.Int, 20, ClassParentID)
            };
            using (SqlDataReader dataReader = data.RunDataReader("dbo.sproc_GetAllChildClass_V2", prams))
            {
                var dt = Tools.ConvertDataReaderToDataTable(dataReader);
                dt.TableName = "TreeView";

                return dt;
            }
        }

        /// <summary>
        /// 初始化TreeView 的 RootNode
        /// </summary>
        private void InitTreeRootNode(TreeNodeCollection parentNodes, DataTable dataTable)
        {
            DataView dataView = dataTable.Copy().DefaultView;
            //	dataView.RowFilter = "ClassParentID = ClassID";
            foreach (DataRowView drv in dataView)
            {
                var classId = (int)drv["ClassID"];
                var node = new TreeNode();
                node.ID = classId.ToString();
                node.Text = "<span onmousemove=javascript:title='" + drv["ClassName"] + "'>" + drv["ClassName"] + "</span>";
                node.ImageUrl = GetIcon((int)drv["ClassType"]);
                node.NavigateUrl = "RightList.aspx?ClassID=" + classId + "&SrcID=" + SrcID + "&DisplayType=" + (int)DisplayType;
                node.Target = TreeViewPage.RightListFrameName;
                parentNodes.Add(node);

                var dt = InitChildNodeDataTable(classId);
                InitTreeChildNode(node.Nodes, classId, dt);
            }

            var globalTreeNode = new TreeNode();
            globalTreeNode.ID = "0";
            globalTreeNode.Text = "<span onmousemove=javascript:title='全局对象'>全局对象</span>";
            globalTreeNode.ImageUrl = GetIcon(-1);
            globalTreeNode.NavigateUrl = "RightList.aspx?ClassID=0&SrcID=" + SrcID + "&DisplayType=" + (int)DisplayType;
            globalTreeNode.Target = TreeViewPage.RightListFrameName;
            parentNodes.Add(globalTreeNode);
        }

        /// <summary>
        /// 初始化TreeView 的 ChildNode
        /// </summary>
        private void InitTreeChildNode(TreeNodeCollection parentNodes, int classParentID, DataTable dataTable)
        {
            DataView dataView = dataTable.Copy().DefaultView;
            dataView.RowFilter = "ClassParentID = " + classParentID + "";
            foreach (DataRowView drv in dataView)
            {
                var classId = (int)drv["ClassID"];
                var node = new TreeNode();
                node.ID = classId.ToString();
                node.Text = "<span onmousemove=javascript:title='" + drv["ClassName"] + "'>" + drv["ClassName"] + "</span>";
                node.ImageUrl = GetIcon((int)drv["ClassType"]);
                node.NavigateUrl = "RightList.aspx?ClassID=" + classId + "&SrcID=" + SrcID + "&DisplayType=" + (int)DisplayType;
                node.Target = TreeViewPage.RightListFrameName;
                parentNodes.Add(node);

                InitTreeChildNode(node.Nodes, classId, dataTable);
            }
        }

        #region 获取节点图标

        /// <summary>
        /// 获取节点图标
        /// </summary>
        private string GetIcon(int type)
        {
            string rtnValue = ResolveUrl("~/DataImages/");
            switch (type)
            {
                case 0:
                    rtnValue += "flag.gif";
                    break;
                case 1:
                    rtnValue += "myDoc.gif";
                    break;
                case 2:
                    rtnValue += "mail.gif";
                    break;
                case 3:
                    rtnValue += "page.gif";
                    break;
                case 4:
                    rtnValue += "staff.gif";
                    break;
                case 5:
                    rtnValue += "help_page.gif";
                    break;
                case 6:
                    rtnValue += "MyTask.gif";
                    break;
                case 7:
                    rtnValue += "mail.gif";
                    break;
                case 8:
                    rtnValue += "myDoc.gif";
                    break;
                case 9:
                    rtnValue += "DocFlow.gif";
                    break;
                case 10:
                    rtnValue += "ClientManage.gif";
                    break;
                case 11:
                    rtnValue += "myLinkman.gif";
                    break;
                case 12:
                    rtnValue += "position.gif";
                    break;
                case 13:
                    rtnValue += "role.gif";
                    break;
                case 14:
                    rtnValue += "kaoqin.gif";
                    break;
                case 15:
                    rtnValue += "workadmin.gif";
                    break;
                case 16:
                    rtnValue += "message.gif";
                    break;
                default:
                    rtnValue += "red_ball.gif";
                    break;
            }
            return rtnValue;
        }

        #endregion
    }
}